﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace NodeBasedSkillEditor.View
{
    public partial class StringField : FieldBase
    {
        public string Value {
            get { return (string)GetValue(ValueProperty); }
            set { if (Value != value) { SetValue(ValueProperty, value); } }
        }
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(StringField),
                new FrameworkPropertyMetadata(
                    "",
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    OnValuePropertyChanged)
                );
        static void OnValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var s = sender as StringField;
            s.InvokeOnValueChanged(e.OldValue, e.NewValue);
        }

        public StringField()
        {
            InitializeComponent();
        }

        public void ApplyValueFromInputBox()
        {
            Value = InputBox.Text;
        }

        protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            base.OnLostKeyboardFocus(e);
            ApplyValueFromInputBox();
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            if (e.Key == Key.Enter && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
            {
                ApplyValueFromInputBox();
                //Value = InputBox.Text;
                e.Handled = true;
            }
        }

    }
}
